<!-- 哪些班级 点进去显示学生的详细信息 -->
<template>
	<base-page class="base-form" title="班级资料" @back="onCancel()">
		<view class="main-box" style="padding: 0rpx 32rpx">
			<view class="content-box">
				<view v-for="(item,index) in dataList" :key="index">
					<uni-card :title="item.classInfo.className" :extra="item.des">
						<view>
							<span class="titleTag">班级期数：</span>
							<span class="titleTag">{{item.classInfo.classPeriods}}</span>
						</view>
						<view>
							<span class="titleTag">创建时间：</span>
							<span class="titleTag">{{item.createTime}}</span>
						</view>
						<view v-show="item.professorId && item.isEvaluate != 1" style="margin: 10px 0">
						</view>
						<view style="display: flex;justify-content: flex-end">
							<span style="font-size: 14px;"
								@click="downloadClick(item.path,item.classInfo.className)"><uni-icons type="download"
									size="24">下载</uni-icons></span>
						</view>
					</uni-card>
				</view>

			</view>
		</view>
	</base-page>
</template>

<script>
	import rules from "@/utils/validate";
	import {
		stringCase,
		treeDataTranslate,
		findItemFromList,
		findTreeNodePath,
		findTreeNode,
		showConfirm,
		showMessage,
		getToken
	} from "@/utils";
	import {
		uploadMixin,
		statsDateRangeMixin
	} from "@/utils/mixins";
	import {
		DropdownWidget,
		TableWidget,
		UploadWidget,
		ChartWidget,
	} from "@/utils/widget.js";
	import {
		Leader
	} from "@/api";

	import projectConfig from '@/core/config'
	export default {
		name: "mobileStudentmyCourse",
		props: {
			subPage: {
				type: [Number, String, Boolean],
				default: 0,
			},
		},
		mixins: [uploadMixin, statsDateRangeMixin],
		data() {
			return {
				codeIMg: '',
				dataList: [],
				formFilter: {
					attendClassTimeFilter: undefined,
				},
				formFilterCopy: {
					attendClassTimeFilter: undefined,
				},
				MobileStudentmyCourse: {
					isInit: false,
				},
				// 表格过滤字段
				sortList: [],
			};
		},
		methods: {
			getUploadFileUrl(item, params) {

				if (item == null || item.downloadUri == null) {
					return null;
				} else {

					const query = {};
					query.Authorization = getToken();
					query.filename = params

					return this.buildGetUrl(item.downloadUri, query);
				}
			},

			buildGetUrl(actionName, params) {
				const queryString = this.objectToQueryString(params);
				if (actionName != null && actionName !== '') {
					if (actionName.substring(0, 1) === '/') actionName = actionName.substring(1);


					return (
						projectConfig.baseUrl + actionName + (queryString == null ? '' : '?' + queryString)
					);
				}
			},

			objectToQueryString(params) {
				if (params == null) {
					return null;
				} else {
					return Object.keys(params)
						.map(key => {
							if (params[key] !== undefined) {
								return `${key}=${params[key]}`;
							} else {
								return undefined;
							}
						})
						.filter(item => item != null)
						.join('&');
				}
			},
			QRcode(val) {
				// const params = {
				// 	studentSignInDto: {
				// 		classId: val.classId,
				// 		courseId: val.id
				// 	},
				// };
				uni.navigateTo({
					url: '/views/generated/qrCode/index?data=' + encodeURIComponent(JSON.stringify(val))
				})
				return
				CourseInfoController.QRcodecreate(this, params)
					.then((res) => {
						this.codeIMg = res
						this.$refs.popup.open(res)
					})
					.catch((e) => {});
			},
			downloadClick(fileUrl, attachmentName) {
				console.log(attachmentName);
				var resObj = JSON.parse(fileUrl)[0];
				var src2 = this.getUploadFileUrl(resObj, resObj.filename);
				// console.log(src2)
				uni.downloadFile({
					//需要预览的文件地址
					url: src2,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.hideLoading();
							//下载成功，得到文件临时地址
							console.log('下载成功1', res.tempFilePath);
							let newUrl = res.tempFilePath
							// 创建一个临时的 <a> 元素用于下载
							try {
								const link = document.createElement('a');
								link.href = newUrl;
								link.setAttribute('download', attachmentName + '.pdf');
								document.body.appendChild(link);
								link.click();
								document.body.removeChild(link);
								URL.revokeObjectURL(link.href);
							} catch (error) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function() {
										uni.showToast({
											title: "文件已保存到手机文件夹下",
											icon: "none"
										})
									}
								})
							}
							uni.showToast({
								title: '下载成功',
								icon: 'none',
							})
							uni.openDocument({
								filePath: res.tempFilePath,
								showMenu: true,
								success: function(file) {
									console.log('file-success', file);
								}
							});
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '下载失败',
								icon: 'none',
							})
						}

					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							title: '下载异常',
							icon: 'none',
						})
					}
				});


			},
			onCancel() {
				uni.navigateBack();
			},
			onRefresh() {
				// this.$refs.cardList.reset();
			},

			onShow() {

			},
			formInit() {
				Leader.getDocList(this)
					.then((res) => {
						console.log(res);
						this.dataList = res
					})
					.catch((e) => {});
			},

		},
		mounted() {
			// 初始化页面数据
			this.formInit();
			this.onRefresh();
		},
	};
</script>

<style scoped>
	.content-box {
		margin: 0 -15px
	}

	.sort-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}

	.sort-item {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.sort-item+.sort-item {
		margin-left: 8px;
	}

	.nav-icon {
		display: inline;
	}

	.nav-icon+.nav-icon {
		margin-left: 5px;
	}

	.text-black {
		width: 70px;
	}


	.titleTag {
		font-size: 30rpx;
		margin-top: 5px;
		color: #303133;
		word-break: break-all;
	}
</style>